// -*- coding: utf-8 -*- 
/**
 * Project: AlgorithmsLearn
 *
 * @author: yanking
 * Create time: 2022-03-31 16:59
 * IDE: IntelliJ IDEA
 * Introduction:
 */
package com.LeetcodeRepeat;

@SuppressWarnings("All")
/**
 * 树状数组构造:单点查询、区间查询、单点修改
 */
public class ArrayTree {
    private int[] a, c;
    private int n;


    int lowbit(int x) {
        return x & (-x);
    }

    public ArrayTree(int[] a, int n) {
        this.a = a;
        this.n = n;
        c = new int[n];
    }

    /*
        单点更新-> 将index索引位置的元素值替换为k
     */
    public void update(int index, int k) {
        while (index <= n) {
            c[index] += k;
            index += lowbit(index);
        }
    }

    public void query(int index) {
        int res = 0;
        while (index > 0) {
            res += c[index];
            index -= lowbit(index);
        }
    }



}
